package com.jacken.springcloudbank02.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jacken.springcloudbank02.common.exception.BizException;
import com.jacken.springcloudbank02.entity.AccountInfo;
import com.jacken.springcloudbank02.entity.SysLog;
import com.jacken.springcloudbank02.mapper.AccountInfoMapper;
import com.jacken.springcloudbank02.mapper.SysLogMapper;
import com.jacken.springcloudbank02.service.IAccountService;
import com.jacken.springcloudbank02.service.ISysLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author wangqiang
 * @version 1.0
 * @date 2020/10/8 9:30
 */
@Service
public class IAccountServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo> implements IAccountService {


    @Resource
    private AccountInfoMapper accountInfoMapper;


    @Resource
    private ISysLogService sysLogService;


    @Resource
    private SysLogMapper sysLogMapper;


    /**
     * 更新账户金额
     *
     * @param accountNo      账户
     * @param accountBalance 金额
     */
    @Override
    @Transactional(rollbackFor = BizException.class)  //开启本地事务  RuntimeException才会回滚 以及Error
    public void updateAccountBalance(String accountNo, Double accountBalance) throws Exception {
        //保存日志
        SysLog sysLog = new SysLog();
        sysLog.setMethodName("updateAccountBalance");
        sysLog.setClassName(this.getClass().getName());
        sysLog.setCreateTime(new Date());
        sysLog.setUpdateTime(new Date());

        //无论如何转账是否成功都想记录操作日志 设置事务隔离级别  这种情况事务不会失效业务service 分开写
        //sysLogService.saveLog(sysLog);

        //调用本地方法事务失效  意思是//挂起之前的事务  新建一个事务
        saveLog(sysLog);
        //增加李四的金额
        accountInfoMapper.updateAccountBalance(accountNo, accountBalance);
        throw new BizException("出现异常拉");

    }


    @Transactional(propagation = Propagation.REQUIRES_NEW)   //挂起之前的事务  新建一个事务
    public void saveLog(SysLog sysLog) {
        sysLogMapper.insert(sysLog);
    }


}
